package common.utils
{
	import mx.collections.ArrayCollection;
	import mx.collections.ArrayList;
	import mx.controls.Alert;
	import mx.rpc.AbstractOperation;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.Operation;
	import mx.rpc.remoting.RemoteObject;
	import mx.rpc.soap.WebService;

	public class AuthorityInvoker
	{
		private static var instance:AuthorityInvoker = null;

		private var tempWindow:Object;

		//用户所拥有本系统所有权限的资源值
		public static var userAllFunctionList:ArrayCollection = new ArrayCollection();

		//private var authRemoteObject:RemoteObject;

		private var securityWS:WebService;

		public static function getInstance() : AuthorityInvoker {
			if( instance == null )
				instance = new AuthorityInvoker();
			return instance;
		}

		public function AuthorityInvoker() {
			//authRemoteObject = new RemoteObject();
			//authRemoteObject.destination = "authorityFacade";
			securityWS = new WebService();
			//securityWS.wsdl = "http://localhost/SCV_APP/SecurityService.asmx?wsdl";
			securityWS.loadWSDL("http://localhost/SCV_APP/SecurityService.asmx?wsdl");
		}


		/**
		 * parentFunctionValue：传"all"返回该用户所有权限的资源值
		 **/
		public function getUserFunctionList(sessionId:String,userId:String,parentFunctionValue:String, securityWindow:Object):void {
			this.tempWindow = securityWindow;

			var oper:AbstractOperation =  securityWS.getOperation("GetFunctionListOfUser");
			oper.addEventListener(ResultEvent.RESULT, resultGetFunctionListOfUser);
			oper.addEventListener(FaultEvent.FAULT, faultMethodHandle);
			oper.send(sessionId,userId,parentFunctionValue);
		}

		public function resultGetFunctionListOfUser(event:ResultEvent):void {
			var list:ArrayCollection = ArrayCollection(event.result.Tables.FunctionValue.Rows);
			this.tempWindow.userFunctionList = list;
		}

		//获取用户所有权限资源值
		public function getUserAllFunctionList(sessionId:String,userId:String):void {

			var oper:AbstractOperation =  securityWS.getOperation("GetFunctionListOfUser");
			oper.addEventListener(ResultEvent.RESULT, resultGetAllFunctionListOfUser);
			oper.addEventListener(FaultEvent.FAULT, faultMethodHandle);
			oper.send(sessionId,userId,"all");
		}

		public function resultGetAllFunctionListOfUser(event:ResultEvent):void {
			var list:ArrayCollection = ArrayCollection(event.result.Tables.FunctionValue.Rows);
			AuthorityInvoker.userAllFunctionList = list;
		}

		public function faultMethodHandle(event : FaultEvent):void {
			Alert.show("call function error");
		}

///////////////////////////////////////////////////////////////////////////////////////////
		public static function isAuthorizedExt(functionValue:String,functionValueList:ArrayCollection):Boolean {
				var index:int = functionValueList.getItemIndex(functionValue);
				return (index >= 0);
		}

		public static function isAuthorized(functionValue:String):Boolean {
		    var flag:Boolean = false;
			var index:int = AuthorityInvoker.userAllFunctionList.getItemIndex(functionValue);
			for (var n:int=0;n<AuthorityInvoker.userAllFunctionList.length;n++) {
				var temp:String = AuthorityInvoker.userAllFunctionList.getItemAt(n).FUNCTION_VALUE;
				if ( temp == functionValue) {
					flag = true;
					break;
				}
			}
			return flag;
		}


		/**
		 *  根据当前RoleGroup的DataGrid中的内容，构造增加和删除的列表
		 *  @beforeChangeNums,初始化选中(保存之前的)的RoleGroup的ID值
		 *  @gridList, DataGrid中所有的列表
		 *  返回：Array[0],是一个二维数组,Array[0][0]是增加的FuntionValue数组,Array[0][0]是增加的DataValue数组
		 *       Array[1],是删除的NUM数组
		 * */
		public static function combin2RoleGroupList(beforeChangeNums:Array,gridList:ArrayCollection):Array {

			var resultArray:Array = new Array();

			var currentSelectedItems:ArrayList = new ArrayList();
			var addRoleFunctionNums:Array = new Array();
			var addRoleDataValues:Array = new Array();
			var addIndex:int =0;

			//构造增加的项
			for (var j:int=0;j<gridList.length;j++) {
				var theItem:Object = gridList.getItemAt(j);
				if (theItem.selected == true) {
					var roleGroupVo:Object = {INTERNAL_NUM:"",INTERNAL_FUNCTION_NUM:""};
					roleGroupVo.INTERNAL_NUM = theItem.INTERNAL_NUM;
					roleGroupVo.INTERNAL_FUNCTION_NUM = theItem.INTERNAL_FUNCTION_NUM;
					//如果当前INTERNAL_NUM为空，则为增加的
					if (theItem.INTERNAL_NUM == null || theItem.INTERNAL_NUM == "") {
						addRoleFunctionNums[addIndex] = int(theItem.INTERNAL_FUNCTION_NUM);
						addRoleDataValues[addIndex] = theItem.DATA_VALUE;
						addIndex ++;
					}
					currentSelectedItems.addItem(roleGroupVo);
				}
			}

			resultArray[0] = new Array();
			resultArray[0][0] = addRoleFunctionNums;
			resultArray[0][1] = addRoleDataValues;

			//构造删除的项
			var delRoleGroupNums:Array = new Array();
			var delIndex:int =0;
			for (var m:int=0;m<beforeChangeNums.length;m++) {
				var assignedNum:String = beforeChangeNums[m];
				//如果在当前选择的范围里面没找到，则为删除的
				var flag:Boolean = false;
				for (var n:int=0;n<currentSelectedItems.length;n++) {
					var selectedNums:String = currentSelectedItems.getItemAt(n).INTERNAL_NUM;
					if (assignedNum == selectedNums) {
						flag = true;
						break;
					}
				}
				if (flag == false) {
					delRoleGroupNums[delIndex] = int(assignedNum);
					delIndex ++;
				}
			}

			resultArray[1] = delRoleGroupNums;
			return resultArray;
		}
	}
}